package com.local.springcloud.controller;

import com.local.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author lou ke
 * @version 1.0
 * @since 2020/7/29 17:22
 */
@RestController
public class DeptConsumerController {

    // 理解，消费者不应该有service层
    // RestTemplate...注册到spring中
    @Autowired
    private RestTemplate restTemplate;

    // 请求地址 http://localhost:8001/dept/selectById
    private static final String REST_URL_PREFIX = "http://localhost:8001"; // 前缀

    @RequestMapping(value = "/consumer/dept/addDept")
    public boolean addDept(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @RequestMapping(value = "/consumer/dept/selectById")
    public Dept selectDeptById(@RequestParam(value = "deptId") Long deptId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/selectById?deptId=" + deptId, Dept.class);
    }

    @RequestMapping(value = "/consumer/dept/selectAll")
    public List<Dept> selectAll() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/selectAll", List.class);
    }


}
